package lec19graph.part0traversal;

import java.util.LinkedList;
import java.util.Queue;

public class GraphBFS {
    final static int INF = Integer.MAX_VALUE >> 1;
    static int n = 5;
    static int[] book = new int[n];
    static int[][] g = new int[][]{
            {0, 1, 1, INF, 1},
            {1, 0, INF, 1, INF},
            {1, INF, 0, INF, 1},
            {INF, 1, INF, 0, INF},
            {1, INF, 1, INF, 0}

    };
    static int sum = 0;//表示访问的节点

    public static void main(String[] args) {
        bfs(0);
    }

    static void bfs(int cur) {
        Queue<Integer> q = new LinkedList<>();
        q.add(cur);
        book[cur] = 1;
        while (!q.isEmpty()) {
            int h = q.peek();
            for (int i = 0; i < n; i++) {
                if (g[h][i] == 1 && book[i] == 0) {
                    q.add(i);
                    book[i] = 1;
                }
            }
            System.out.println(h);
            q.poll();
        }
    }


}
